package com.spice.service.entity.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.spice.common.constant.PermissionConstant;
import com.spice.common.exception.SpiceException;
import com.spice.common.model.BaseEntity;
import com.spice.service.util.MultipartFileUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "文档信息")
public class FileInfo extends BaseEntity {

    public FileInfo(String originalFileName, String localFileName) {
        this.originalFileName = originalFileName;
        this.localFileName = localFileName;
    }

    @ApiModelProperty(value = "原始文件名", example = "xxxx")
    private String originalFileName;

    @ApiModelProperty(value = "本地文件名", example = "xxxx-timestamp")
    private String localFileName;

    @ApiModelProperty(value = "下载地址", example = "ip:port/api/utils/downloadFile/xxxx-timestamp")
    private String downloadUrl;

//    @ApiModelProperty(value = "文件内容地址", example = "yhm20010921")
//    private String fileContentUrl;



    public FileInfo loadUrl(HttpServletRequest request) {
        String url = request.getRequestURL()
                .substring(0, request.getRequestURL().length()
                        - request.getRequestURI().length());
//        this.fileContentUrl = url + "/api/utils/getFileContent/" + localFileName;
        this.downloadUrl = url + "/api/utils/downloadFile/" + localFileName;
        return this;
    }

    public boolean isFileExist(){
        return MultipartFileUtil.isFileExist(localFileName);
    }
}
